<?php

namespace app\core;

use Swoole\Http\Request as REQ;
use app\core\DB\Test\{Db,DBPoolContext};

class Request
{

    const HEADER = 'REQUEST.HEADER';
    const REQ_OBJ = 'REQUEST.REQ_OBJ';
    const PARAMS = 'REQUEST.PARAMS';
    const SERVER = 'REQUEST.SERVER';
    const FILE = 'REQUEST.FILE';
    const DB = 'REQUEST.DB';
    public static function param(string $name = '', mixed $default = null, string $filters = '')
    {
        if (empty($name)) {
            return Context::get(self::PARAMS);
        }
        $data = self::getParam($name);
       
        if (!empty($data)) {
            if (!empty($filters)) {
                foreach (explode(',', $filters) as $k => $v) {
                    $data = $v($data);
                }
            }
        } else {
            $data = $default;
        }
        return $data;
    }

    public static function setParam(array $data)
    {

        $PARAMS = static::PARAMS;
        Context::put($PARAMS, $data);
        // foreach ($data as $k => $v) {
        //     self::put($k, $v);
        // }

        return new self;
    }
    public static function getParam(string $name)
    {
        $param  = Context::get(self::PARAMS);
        $name = explode('.', $name);

        // 按.拆分成多维数组进行判断
        foreach ($name as $val) {
            if (isset($param[$val])) {
                $param = $param[$val];
            }
        }

        return $param;
    }

    public static function params(){
        return  Context::get(self::PARAMS);
    }
    public static function headers(){
        return Context::get(static::HEADER);
    }
    public static function init(REQ $req)
    {
        $key = self::REQ_OBJ;
        Context::put($key, $req);
        $raw_get = $req->get ?? [];
        $raw_body = empty($req->getContent()) ? [] : json_decode($req->getContent(), true);
        $raw_post = $req->post ?? [];
        $files = $req->files ?? [];
        $db=Db::getInstance();
       
        if (empty($raw_body)) {
            $raw_body = [];
        }

        $raw_data = array_merge($raw_get, $raw_post, $raw_body);
        static::setDb($db)->setParam($raw_data)->setFile($files)->setHeader($req->header)->setServer($req->server);
    }
    public function setHeader($item)
    {
        $key = static::HEADER;
        Context::put($key, $item);
        return new self;
    }
    public static function getHeader(?string $name='')
    {
        $key = static::HEADER;
        if (empty($name)) {
            return Context::get($key);
        }
        return Context::get($key)[$name];
    }
    public function setFile(mixed $item)
    {
        $key = static::FILE;
        Context::put($key, $item);
        return new self;
    }
    public static function getFile(string $key)
    {
        $name = self::FILE;

        return Context::get($name)[$key];
    }
    public function setServer($item)
    {
        $key = static::SERVER;
        Context::put($key, $item);
        return new self;
    }
    public static function getServer()
    {
        $key = static::HEADER;
        return Context::get($key);
    }
    public static function setDb($item){
        $key = static::DB;
        Context::put($key, $item);
        return new self;
    }
    public static function getDb(){
        $key = static::DB;
        return Context::get($key);
    }


}
